﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModelBase;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Main;
using Infragistics.Win.UltraWinTabbedMdi;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Main
{
    public partial class MainViewModel : ObservableObject
    {
        
        #region Private properties

        /// <summary>
        /// Referencia de la vista.
        /// <see cref="Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Main.MainView"/>
        /// </summary>
        private MainView _view;

        private Dictionary<string, MdiTab> _tabs;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructor por defecto.
        /// </summary>
        /// <param name="view">
        /// <see cref="Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Main.MainView"/>
        /// </param>
        public MainViewModel(MainView view)
        {
            if (view == (MainView)null)
                throw new ArgumentNullException("view");
            _tabs = new Dictionary<string, MdiTab>();
            _view = view;
            WireUpEvents();
            InitModel();
        }

        #endregion

        #region Event methods

        /// <summary>
        /// Suscribe los elementos de la vista a los
        /// eventos correspondientes.
        /// </summary>
        private void WireUpEvents()
        {
            PropertyChanged += MainViewModel_PropertyChanged;
            _view.Load += LoadView;
			_view.mainToolbarsManager.ToolClick += HandleMenu;
			_view.mainTabbedMdiManager.TabClosing += ClosingView;
            _view.FormClosing += CloseView;
            _view.synchronizationTimer.Tick += CheckSynchronization;
            _view.synchronizationBackgroundWorker.DoWork += Synchronize;
            _view.synchronizationBackgroundWorker.RunWorkerCompleted += NotifySynchronizationSummary;
        }

        private void CloseView(object sender, CancelEventArgs e)
        {
            if (_view.synchronizationBackgroundWorker.IsBusy)
            {
                e.Cancel = _closeAfterSynchronization = true;
                MessageBox.Show(_view, Messages.Message_CloseAfterSynchronization, Messages.Message_ActiveSynchronization, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void LoadView(object sender, EventArgs e)
        {
            _view.Text = string.Format(
                CultureInfo.InvariantCulture,
                WindowTitles.MainWindowTitle
            );
        }

        #endregion

        #region Private methods

        private void InitModel()
        {
            StartPermisosModuleService();
        }

        private void StartPermisosModuleService()
        {
            DistributedServicesPermisosModuleApplicationContext.InitializePermisosModuleServiceHost();
        }

        private void StopPermisosModuleService()
        {
            DistributedServicesPermisosModuleApplicationContext.StopPermisosModuleServiceHost();
        }

        #endregion

    }
}
